﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_AddType : ATPages
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hidID1.Value = Request.QueryString["moduid"].ToString();
            lblTime.Text = DateTime.Now.ToShortDateString();
            lblUser.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            lblwz.Text = f.getModuleidAdd(Request["moduid"]);
            txtbh.Text = DateTime.Now.ToString();
            showconmi();
            txtbh.Attributes.Add("onclick", "SelectDate('txtbh')");
            ddlComodName();
            if (Request["action"] == "zk")
            {
                Label6.Text = "转库仓库：";
                Label1.Text = "转库数量：";
                Label2.Text = "转库时间：";
                Label3.Text = "转库用途：";
                Label4.Text = "备注：";
                Label5.Text = "转库人：";
                btnSave.Text = " 转库 ";
            }
            if (Request["action"] == "jh")
            {
                Label1.Text = "进货数量：";
                Label2.Text = "进货时间：";
                Label3.Text = "货物用途：";
                Label4.Text = "备注：";
                Label5.Text = "进货人：";
                btnSave.Text = " 进货 ";
            }
            if (Request["action"] == "th")
            {
                Label1.Text = "退货数量：";
                Label2.Text = "退货时间：";
                Label3.Text = "退货原因：";
                Label4.Text = "备注：";
                Label5.Text = "退货人：";
                btnSave.Text = " 退货 ";
            }

        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request["id"]))
        {
            if (Request["action"] == "zk")
            {
                ZSql seleccomi = new ZSql();
                seleccomi.Open("select * from CS_Stock where stockId=" + Request["id"] + " and warehouseid=" + Request["warehouseid"] + " ");
                if (seleccomi.m_table.Rows[0]["WarehouseId"].ToString() != ddlComodiName.SelectedValue)
                {
                    ZSql bjck = new ZSql();
                    string  num = "";
                    if (string.IsNullOrEmpty(txtType.Text))
                    {
                        num = seleccomi.m_table.Rows[0]["TotalNum"].ToString();
                    }
                    else
                    {
                        num = txtType.Text;
                    }
                    bjck.Open("insert into CS_zk (deptid,GuestId,MaintDate,MaintCode,SalesCode,DevicState,AuditDate,AuditUserName,housenameid) values(" + Request["id"] + ",'" + num + "','" + Common.FormatParameter(txtbh.Text) + "','" + Common.FormatParameter(txtOrder.Text) + "','" + Common.FormatParameter(txtDescription.Text) + "','" + lblUser.Text + "','" + lblTime.Text + "','2'," + ddlComodiName.SelectedValue + ")");
                    if (string.IsNullOrEmpty(txtType.Text))
                    {

                        ZSql numupdate = new ZSql();
                        numupdate.Open("update CS_Stock set WarehouseId=" + ddlComodiName.SelectedValue + " where stockId=" + Request["id"] + " and warehouseid=" + Request["warehouseid"] + "");
                        numupdate.Close();
                        ClientScript.RegisterStartupScript(typeof(Page), "aa", "selectInfo(" + Request["moduid"] + ")", true);

                    }
                    else
                    {
                        ZSql seleccomcou = new ZSql();
                        ZSql houseupdate = new ZSql();
                        ZSql houseupdate1 = new ZSql();
                        ZSql addcomi = new ZSql();

                        if (seleccomi.m_table.Rows.Count > 0)
                        {
                            //if (Convert.ToInt32(seleccomi.m_table.Rows[0]["TotalNum"]) >= Convert.ToInt32(txtType.Text))//数量大于转库数时
                            //{
                                seleccomcou.Open("select * from CS_Stock where WarehouseId='" + ddlComodiName.SelectedValue + "' and ModelId='" + seleccomi.m_table.Rows[0]["ModelId"] + "'and  ComoditiesType<>null and ComoditiesId<>null ");
                                if (seleccomcou.m_table.Rows.Count > 0)
                                {
                                    houseupdate.Open("update CS_Stock set TotalNum=TotalNum+" + Convert.ToInt32(txtType.Text) + " where WarehouseId='" + ddlComodiName.SelectedValue + "' and ModelId='" + seleccomi.m_table.Rows[0]["ModelId"] + "'");

                                }
                                else
                                {
                                    addcomi.Open("insert into CS_Stock(ComoditiesId,WarehouseId,ComoditiesType,ComodBarcode,BrandId,ComoditiesName,ModelId,UnitId,SuppliId,TotalNum,Prices,ProcNum,storageTime,AuditUserName,AuditDate,typeStock,ProcMoney,SuppliMoney,ChainMoney,WholeMoney1,WholeMoney2,RetailMoney,RestMoney,ActiveMoney,newSuppliMoney) values(" + Convert.ToInt32(seleccomi.m_table.Rows[0]["ComoditiesId"]) + ",'" + ddlComodiName.SelectedValue + "','" + seleccomi.m_table.Rows[0]["ComoditiesType"].ToString() + "','" + seleccomi.m_table.Rows[0]["ComodBarcode"].ToString() + "','" + seleccomi.m_table.Rows[0]["BrandId"].ToString() + "','" + seleccomi.m_table.Rows[0]["ComoditiesName"].ToString() + "','" + seleccomi.m_table.Rows[0]["ModelId"].ToString() + "','" + seleccomi.m_table.Rows[0]["UnitId"].ToString() + "','" + seleccomi.m_table.Rows[0]["SuppliId"].ToString() + "'," + Convert.ToInt32(txtType.Text) + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["Prices"]) + "," + Convert.ToInt32(txtType.Text) + ",'" + Convert.ToDateTime(seleccomi.m_table.Rows[0]["storageTime"]) + "','" + seleccomi.m_table.Rows[0]["AuditUserName"].ToString() + "','" + Convert.ToDateTime(seleccomi.m_table.Rows[0]["AuditDate"]) + "','" + seleccomi.m_table.Rows[0]["typeStock"].ToString() + "'," + Convert.ToDouble(seleccomi.m_table.Rows[0]["ProcMoney"]) + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["SuppliMoney"]) + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["ChainMoney"]) + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["WholeMoney1"]) + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["WholeMoney2"]) + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["RetailMoney"]) + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["RestMoney"]) + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["ActiveMoney"]) + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["newSuppliMoney"]) + ")");
                                    houseupdate.Open("update CS_Stock set TotalNum=TotalNum-" + Convert.ToInt32(txtType.Text) + " where stockId=" + Request["id"] + " and warehouseid=" + Request["warehouseid"] + "");
                                }
                            //}
                            //else
                            //{
                               // ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('库存数量不对！')", true);
                            //}

                        }
                        ClientScript.RegisterStartupScript(typeof(Page), "aa", "selectInfo(" + Request["moduid"] + ")", true);
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('同一仓库不能转换！')", true);
                }
            }
            if (Request["action"] == "jh")
            {
                ZSql bjck = new ZSql();
                bjck.Open("insert into CS_Outbj (deptid,GuestId,MaintDate,MaintCode,SalesCode,DevicState,AuditDate,AuditUserName) values(" + Request["id"] + ",'" + Common.FormatParameter(txtType.Text) + "','" + Common.FormatParameter(txtbh.Text) + "','" + Common.FormatParameter(txtOrder.Text) + "','" + Common.FormatParameter(txtDescription.Text) + "','" + lblUser.Text + "','" + lblTime.Text + "','1')");
                ZSql numupdate = new ZSql();
                numupdate.Open("update CS_Stock set TotalNum=TotalNum+" + Convert.ToInt32(txtType.Text) + " where stockId=" + Request["id"] + "");
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "selectInfo(" + Request["moduid"] + ")", true);
            }
            if (Request["action"] == "th")
            {
                ZSql bjck = new ZSql();
                bjck.Open("insert into CS_Outbj (deptid,GuestId,MaintDate,MaintCode,SalesCode,DevicState,AuditDate,AuditUserName) values(" + Request["id"] + ",'" + Common.FormatParameter(txtType.Text) + "','" + Common.FormatParameter(txtbh.Text) + "','" + Common.FormatParameter(txtOrder.Text) + "','" + Common.FormatParameter(txtDescription.Text) + "','" + lblUser.Text + "','" + lblTime.Text + "','3')");
                ZSql numupdate = new ZSql();
                numupdate.Open("update CS_Stock set TotalNum=TotalNum+" + Convert.ToInt32(txtType.Text) + " where stockId=" + Request["id"] + "");
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "selectInfo(" + Request["moduid"] + ")", true);
            }
        }     
    }
    protected void showconmi()
    {
        ZSql showdate = new ZSql();
        string sql = "select * from (select*, (select top 1 stockId from CS_Stock where tableKC.ModelId=ModelId and tableKC.warehouseid=warehouseid order by stockId desc) as stockId from (select WarehouseName,a.ModelId,d.Brand,e.ComoditiesName,f.modulbh,c.typebh,c.ComoditiesType,a.warehouseid,f.Specifications,j.Units,h.Supplier,ComodBarcode,sum(TotalNum)as num from CS_stock a join CS_Warehouse b on a.warehouseid=b.WarehouseId join ComoditiesType c on a.ComoditiesType=c.TypeId join Brand d on a.BrandId=d.BrandId join ComoditiesName e on a.ComoditiesName=e.ComoditiesNameId join Specifications f on a.ModelId=f.SpecificationsId join Units j on a.UnitId=j.UnitsId join CS_Supplier h on a.SuppliId=h.SupplierId where a.WarehouseId<>'24' group by ComodBarcode,d.brand,WarehouseName,e.ComoditiesName,f.Specifications,h.Supplier,j.Units,a.ModelId,a.warehouseid,c.ComoditiesType,f.modulbh,c.typebh) as tableKC ) as tableStock where  stockId=" + Request["id"] + " and warehouseid=" + Request["warehouseid"] + " order by warehousename ";
        //string sql = "select a.stockId,WarehouseName,d.Brand,e.ComoditiesName,f.Specifications,f.modulbh,j.Units,a.warehouseid,c.ComoditiesType,c.typebh,h.Supplier,ComodBarcode,sum(TotalNum)as num from CS_stock a join CS_Warehouse b on a.warehouseid=b.WarehouseId join ComoditiesType c on a.ComoditiesType=c.TypeId join Brand d on a.BrandId=d.BrandId join ComoditiesName e on a.ComoditiesName=e.ComoditiesNameId join Specifications f on a.ModelId=f.SpecificationsId join Units j on a.UnitId=j.UnitsId join CS_Supplier h on a.SuppliId=h.SupplierId   where a.WarehouseId<>'24' and stockId=" + Request["id"] + " and a.warehouseid=" + Request["warehouseid"] + "  group by ComodBarcode,d.brand,WarehouseName,e.ComoditiesName,f.Specifications,f.modulbh,h.Supplier,c.ComoditiesType,c.typebh,j.Units,a.stockId,a.warehouseid order by warehousename";
        showdate.Open(sql);
        if (showdate.m_table.Rows.Count > 0)
        {
            Label7.Text = showdate.m_table.Rows[0]["WarehouseName"].ToString();
            Label8.Text = showdate.m_table.Rows[0]["Brand"].ToString();
            Label9.Text = showdate.m_table.Rows[0]["ComoditiesName"].ToString();
            Label10.Text = showdate.m_table.Rows[0]["Specifications"].ToString();
            Label11.Text = showdate.m_table.Rows[0]["num"].ToString();
        }
    }
    protected void ddlComodName()
    {
        //ZSql name = new ZSql();
        //name.Open("select * from CS_Warehouse where AuditState='已通过'");
        //ddlComodiName.DataSource = name.m_table;
        //ddlComodiName.DataTextField = "WarehouseName";
        //ddlComodiName.DataValueField = "WarehouseId";
        //ddlComodiName.DataBind();
        //name.Close();
        //ddlComodiName.Items.Insert(0, new ListItem("—选择收货仓库—", "0"));
        ZSql ware = new ZSql();
        ware.Open("select warehouseid,warehousename from CS_Warehouse");
        if (ware.m_table.Rows.Count > 0)
        {
            ware.Open("select warehouseid,warehousename from CS_Warehouse where AuditState='已通过'");
            if (ware.m_table.Rows.Count > 0)
            {
                ddlComodiName.DataSource = ware.m_table;
                ddlComodiName.DataTextField = "warehousename";
                ddlComodiName.DataValueField = "warehouseid";
                ddlComodiName.DataBind();
                ddlComodiName.Items.Insert(0, new ListItem("—选择仓库—", "0"));

                ware.Close();
            }
            else
            {
                Response.Write("<script>alert('仓库未审核！');</script>");
                ddlComodiName.Items.Insert(0, new ListItem("—选择仓库—", "0"));
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
        }
        else
        {
            Response.Write("<script>alert('仓库为空请添加仓库！');</script>");
            ddlComodiName.Items.Insert(0, new ListItem("—选择仓库—", "0"));
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
        }


    }
    
}
